package com.jakewu.horizontalgridview.data;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v17.leanback.widget.Presenter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewu.horizontalgridview.R;
import com.jakewu.horizontalgridview.utils.Utils;
import com.jakewu.horizontalgridview.widgets.EntryItemView;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

import java.net.URI;
import java.net.URISyntaxException;

import static com.squareup.picasso.MemoryPolicy.NO_CACHE;
import static com.squareup.picasso.MemoryPolicy.NO_STORE;

public class EntryPresenter extends Presenter {

    public static String TAG = EntryPresenter.class.getName();
    private int CARD_WIDTH = Utils.getCardContext()
            .getResources().getDimensionPixelOffset(R.dimen.normal_card_width);
    private int CARD_HEIGHT = Utils.getCardContext()
            .getResources().getDimensionPixelOffset(R.dimen.normal_card_height);


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {

        Context context = parent.getContext();
        EntryItemView entry = new EntryItemView(context);
        entry.setFocusable(true);
        entry.setFocusableInTouchMode(true);
        entry.setMainImageDimensions(CARD_WIDTH, CARD_HEIGHT);

        return new ViewHolder(entry);
    }


    @Override
    public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item) {

        if(item instanceof Entries) {

            Entries entries = (Entries) item;
            if (entries.getEntryImageUrl() != null) {
                if (entries.getTitle() != null) {
                    ((ViewHolder) viewHolder).mEntryView.setTitleText(entries.getTitle());
                } else {
                    ((ViewHolder) viewHolder).mEntryView.setTitleText("");
                }
                if (entries.getDesc() != null) {
                    ((ViewHolder) viewHolder).mEntryView.setContentText(entries.getDesc());
                } else {
                    ((ViewHolder) viewHolder).mEntryView.setContentText("");
                }
                Uri uri = Uri.parse(entries.getEntryImageUrl());
                Log.v(TAG, "uri:" + uri);
                try {
                    ((ViewHolder) viewHolder).updateCardViewImage(new URI(uri.toString()), CARD_WIDTH, CARD_HEIGHT);
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    @Override
    public void onUnbindViewHolder(Presenter.ViewHolder viewHolder) {
        ((ViewHolder)viewHolder).mEntryView.setMainImage(null);
        if(null != Utils.getPicassoInstance()) {
            Utils.getPicassoInstance().cancelRequest(((ViewHolder) viewHolder).mEntryViewTarget);
        }
    }


    static class ViewHolder extends Presenter.ViewHolder {

        private EntryItemView mEntryView;
        private PicassoImageCardViewTarget mEntryViewTarget;

        public ViewHolder(View view) {
            super(view);
            mEntryView = (EntryItemView) view;
            mEntryViewTarget = new PicassoImageCardViewTarget(mEntryView);
        }

        protected void updateCardViewImage(URI uri, int width, int height) {
            if((null != Utils.getPicassoInstance())
                    && (null != Utils.getCardContext())) {
                Utils.getPicassoInstance().load(uri.toString())
                        .resize(width,height)
                        .centerCrop()
                        .memoryPolicy(NO_CACHE, NO_STORE)
                        .into(mEntryViewTarget);
            }
        }
    }

    public static class PicassoImageCardViewTarget implements Target {
        private EntryItemView mEntryItemView;

        public PicassoImageCardViewTarget(EntryItemView entryItemView) {
            mEntryItemView = entryItemView;
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
            if(null != Utils.getCardContext()) {
                Drawable bitmapDrawable = new BitmapDrawable(Utils.getCardContext().getResources(), bitmap);
                mEntryItemView.setMainImage(bitmapDrawable);
            }
        }

        @Override
        public void onBitmapFailed(Drawable drawable) {
            mEntryItemView.setMainImage(drawable);
        }

        @Override
        public void onPrepareLoad(Drawable drawable) {
            // Do nothing, default_background manager has its own transitions
        }
    }
}
